// 该文件专门用于创建整个应用的路由器
import VueRouter from "vue-router";
import About from "@/pages/About.vue";
import Home from "@/pages/Home.vue";
import News from "@/pages/News.vue";
import Message from "@/pages/Message.vue";
import Detail from "@/pages/Detail.vue";


const router = new VueRouter({
    routes:[
        {
            name:"guanyu",
            path:'/about',
            component:About,
            meta:{
                title:'关于'
            }
        },
        {
            name:"shouye",
            path:'/home',
            component:Home,
            meta:{
                title:'首页'
            },
            children:[
                {
                    name:"xinwen",
                    path:'news',
                    component:News,
                    meta:{
                        isAuth:true,
                        title:'新闻'
                    },
                    beforeEnter (to, from, next) {//独享路由守卫
                        if(to.meta.isAuth){
                            if(localStorage.getItem('school')==='MEO'){
                                next()
                            }else{
                                alert("无权限查看")
                            }
                        }else{
                            console.log("=========")
                            next()
                        }
                    }
                },
                {
                    name:"xiaoxi",
                    path:'message',
                    component:Message, 
                    meta:{
                        isAuth:true,
                        title:'消息'
                    },
                    children:[
                        {
                            name:"xiangqing",
                            path:"detail",
                            component:Detail,
                            meta:{
                                title:'详情'
                            },
                            props({query:{id,title}}){
                                return {
                                    id:id,
                                    title:title
                                }
                            }
                        }
                    ]
                }
            ]
        }
    ]
})
/*
//全局前置路由守卫。初始化以及每次路由切换之前调用
router.beforeEach((to,from,next)=>{
    console.log('到哪去',to)//到哪去
    console.log('从哪来',from)//从哪来
    
    if(to.meta.isAuth){
        if(localStorage.getItem('school')==='MEO'){
            next()
        }else{
            alert("无权限查看")
        }
    }else{
        console.log("=========")
        next()
    }
})
*/
//全局后置路由守卫。初始化以及每次路由切换之后调用
router.afterEach((to,from)=>{
    console.log('到哪去',to)//到哪去
    console.log('从哪来',from)//从哪来

    document.title = to.meta.title||""
})

export default router